package com.princess.demok.ui.demo

import android.app.Activity
import android.content.Intent
import android.util.Log
import android.view.MotionEvent
import android.widget.RadioGroup
import com.princess.baselib.utils.LogUtils
import com.princess.baselib.utils.setOnClickListenerWithTrigger
import com.princess.demok.R
import com.princess.demok.base.view.MyBaseActivity
import kotlinx.android.synthetic.main.activity_touch.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * @author: xha
 * @date: 2021/9/16 11:43
 * @Description:
 */
@Suppress("UNREACHABLE_CODE")
class TouchActivity : MyBaseActivity() {


    var activityDispatchTouchEventInt: Int = 0
    var activityOnTouchEventInt: Int = 0
    var viewGroupDispatchTouchEventInt: Int = 0
    var viewGroupOnInterceptTouchEventInt: Int = 0
    var viewGroupOnTouchEventInt: Int = 0

    companion object {
        @JvmStatic
        fun start(
            context: Activity
        ) {
            val starter = Intent(context, TouchActivity::class.java)
            context.startActivity(starter)
        }
    }

    override fun getLayoutId(): Int? {
        return R.layout.activity_touch
    }

    override fun initOnCreate() {

    }

    override fun initOnClickerListener() {
        super.initOnClickerListener()
        tvTouch.setOnClickListenerWithTrigger {
            LogUtils.i("touch", "view点击事件")
        }

        radioGroup.setOnCheckedChangeListener { group, checkedId ->
            run {
                showType(group, checkedId)
                LogUtils.i("----------------------------------------------")
            }
        }
        radioGroup2.setOnCheckedChangeListener { group, checkedId ->
            run {
                showType(group, checkedId)
                LogUtils.i("----------------------------------------------")
            }
        }
        radioGroup3.setOnCheckedChangeListener { group, checkedId ->
            run {
                showType(group, checkedId)
                myRlTouch.viewGroupDispatchTouchEventInt = this.viewGroupDispatchTouchEventInt
                LogUtils.i("----------------------------------------------")
            }
        }
        radioGroup4.setOnCheckedChangeListener { group, checkedId ->
            run {
                showType(group, checkedId)
                myRlTouch.viewGroupOnInterceptTouchEventInt = this.viewGroupOnInterceptTouchEventInt
                LogUtils.i("----------------------------------------------")
            }
        }

    }

    private fun showType(group: RadioGroup?, checkedId: Int) {
        when (checkedId) {
            R.id.activitSuper -> {
                activityDispatchTouchEventInt = 0
            }
            R.id.activitTrue -> {
                activityDispatchTouchEventInt = 1
            }
            R.id.activitFalse -> {
                activityDispatchTouchEventInt = 2
            }
            R.id.activityOnTouchEventSuper -> {
                activityOnTouchEventInt = 0
            }
            R.id.activityOnTouchEventTrue -> {
                activityOnTouchEventInt = 1
            }
            R.id.activityOnTouchEventFalse -> {
                activityOnTouchEventInt = 2
            }
            R.id.viewGroupDispatchTouchEventSuper -> {
                viewGroupDispatchTouchEventInt = 0
            }
            R.id.viewGroupDispatchTouchEventTrue -> {
                viewGroupDispatchTouchEventInt = 1
            }
            R.id.viewGroupDispatchTouchEventFalse -> {
                viewGroupDispatchTouchEventInt = 2
            }
            R.id.viewGroupOnInterceptTouchEventSuper -> {
                viewGroupOnInterceptTouchEventInt = 0
            }
            R.id.viewGroupOnInterceptTouchEventTrue -> {
                viewGroupOnInterceptTouchEventInt = 1
            }
            R.id.viewGroupOnInterceptTouchEventFalse -> {
                viewGroupOnInterceptTouchEventInt = 2
            }
        }
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        return when (activityDispatchTouchEventInt) {
            0 -> {
                LogUtils.i("touch", "Activity dispatchTouchEvent  " + super.dispatchTouchEvent(ev))
                super.dispatchTouchEvent(ev)
            }
            1 -> {
                LogUtils.i("touch", "Activity dispatchTouchEvent  " + true)
                true
            }
            else -> {
                LogUtils.i("touch", "Activity dispatchTouchEvent  " + false)
                false
            }
        }


    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return when (activityOnTouchEventInt) {
            0 -> {
                LogUtils.i("touch", "Activity onTouchEvent  " + super.onTouchEvent(event))
                super.onTouchEvent(event)
            }
            1 -> {
                LogUtils.i("touch", "Activity onTouchEvent  " + true)
                true
            }
            else -> {
                LogUtils.i("touch", "Activity onTouchEvent  " + false)
                false
            }

        }
    }
}